package com.zhongke.DKYCboot.service.impl;

import com.zhongke.DKYCboot.mapper.BaseCustomerMapper;
import com.zhongke.DKYCboot.bean.BaseCustomer;
import com.zhongke.DKYCboot.service.BaseCustomerService;
import com.zhongke.DKYCboot.util.AllUtils;
import com.zhongke.DKYCboot.util.Msg;
import com.zhongke.DKYCboot.vo.BaseCustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BaseCustomerServiceimpl implements BaseCustomerService {


    @Autowired
    private BaseCustomerMapper baseCustomerMapper;

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    public Msg selectBaseById(int id) {
        List<BaseCustomerVo> list = baseCustomerMapper.selectBaseById(id);
        if (AllUtils.isNotEmpty(list)){
            return Msg.success(list);
        }else {
            return Msg.fail("查询失败");
        }


    }





    /**
     * 新增用户
     * @param request
     * @return
     */
    public Msg insertBaseUser(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        if (AllUtils.isNotEmpty(request.getParameter("job"))){
            map.put("job",request.getParameter("job"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("site"))){
            map.put("site",request.getParameter("site"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("address"))){
            map.put("address",request.getParameter("address"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("idnumber"))){
            map.put("idnumber",request.getParameter("idnumber"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("birth"))){
            map.put("birth",request.getParameter("birth"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("email"))){
            map.put("email",request.getParameter("email"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("idcards"))){
            map.put("idcards",request.getParameter("idcards"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("idcardsb"))){
            map.put("idcardsb",request.getParameter("idcardsb"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("workadress"))){
            map.put("workadress",request.getParameter("workadress"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("id"))){
            map.put("id",request.getParameter("id"));
        }
        int resu = baseCustomerMapper.insertBaseUser(map);
        if (resu==1){
            return Msg.success("添加成功");
        }else {
            return Msg.fail("添加失败");
        }
    }


    public Msg updateBaseUser(HttpServletRequest request) {

        Map<String,Object> map = new HashMap<>();
        if (AllUtils.isNotEmpty(request.getParameter("name"))){
            map.put("name",request.getParameter("name"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("idnumber"))){
            map.put("idnumber",request.getParameter("idnumber"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("site"))){
            map.put("site",request.getParameter("site"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("address"))){
            map.put("address",request.getParameter("address"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("job"))){
            map.put("job",request.getParameter("job"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("workadress"))){
            map.put("workadress",request.getParameter("workadress"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("birth"))){
            map.put("birth",request.getParameter("birth"));
        }
        if (AllUtils.isNotEmpty(request.getParameter("id"))){
            map.put("id",request.getParameter("id"));
        }
        int resu = baseCustomerMapper.updateBaseUser(map);
        if (resu==1){
            return Msg.success("修改成功");
        }else {
            return Msg.fail("修改失败");
        }
    }

    @Override
    public Msg deleteBaseUser(int id) {
        int resu = baseCustomerMapper.deleteBaseUser(id);
        if (resu==1){
            return Msg.success("删除成功");
        }
        else{
            return Msg.fail("删除失败");
        }
    }

    @Override
    public Msg findAll(BaseCustomerVo baseCustomer) {
        List<BaseCustomerVo> list = baseCustomerMapper.findAll(baseCustomer);
        if (list !=null){
            return Msg.success(list);
        }
        else{
            return Msg.fail("查询失败");
        }

    }

    @Override
    public Msg login(String tel, String password) {
        BaseCustomer baseCustomer = baseCustomerMapper.login(tel, password);
        if (baseCustomer != null) {
            BaseCustomer baseCustomer1 =new BaseCustomer();
            baseCustomer1.setToken("admin-token");
            return Msg.success(baseCustomer1);
        } else {
            return Msg.fail("登录失败");
        }
    }
}